#ifndef LORA_INTERFACE_H
#define LORA_INTERFACE_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>


#define LORA_BUFFER_SIZE  (256)
#pragma pack(1)
typedef struct
{
    uint32_t addr;
    uint32_t addrh;
} id32_t;
typedef union
{
    id32_t id32;
    uint64_t id64;
    uint8_t id8[8];
} dev_id_u;

enum
{
    LORA_CMD_IDLE     = 0,
    LORA_CMD_ADDR_REQ = 1,
    LORA_CMD_ADDR_RSP = 2,
    LORA_CMD_TEST_REQ = 3,
    LORA_CMD_TEST_RSP = 4,
    LORA_CMD_MAX
};

typedef struct
{
    uint8_t data;
} lora_cmd_addr_req_t;

typedef struct
{
    uint8_t data;
} lora_cmd_addr_rsp_t;

typedef struct
{
    uint8_t data;
} lora_cmd_test_req_t;

typedef struct
{
    uint8_t data[32];
} lora_cmd_test_rsp_t;

typedef union
{
    lora_cmd_addr_req_t cmd_addr_req;
    lora_cmd_addr_rsp_t cmd_addr_rsp;
    lora_cmd_test_req_t cmd_test_req;
    lora_cmd_test_rsp_t cmd_test_rsp;
    uint8_t data[LORA_BUFFER_SIZE - 11];
} lora_body_u;

typedef struct
{
    uint32_t src;
    uint32_t dst;
    uint8_t cmd;  /* cmd*/
    uint8_t len;
} lora_header_t;

typedef struct
{
    lora_header_t head;
    lora_body_u body;
} lora_msg_t;

typedef union
{
    lora_msg_t msg;
    uint8_t buf[LORA_BUFFER_SIZE];
} lora_buffer_u;

#pragma pack()

int32_t lora_random(void);
void lora_recv_mode(void);
void lora_idle_mode(void);

void lora_rx_error(void);

bool lora_client_send_active(void);
void lora_client_send(void);
bool lora_client_handle_recv(void);

bool lora_server_send_active(void);
void lora_server_send(void);
bool lora_server_handle_recv(void);

#ifdef __cplusplus
}
#endif
#endif // LORA_INTERFACE_H
